package com.zbkj.crmebadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.zbkj.crmebadmin.vo.UserExcelVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
@RestController
public class ExportExcelExample {
    @GetMapping("/api/export/users")
    public void exportUsers(HttpServletResponse response) throws Exception {
        // 构造模拟数据
        List<UserExcelVO> users = Arrays.asList(
                new UserExcelVO(1, "张三", "zhangsan@example.com"),
                new UserExcelVO(2, "李四", "lisi@example.com")
        );

        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("用户列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 写入Excel
        EasyExcel.write(response.getOutputStream(), UserExcelVO.class)
                .sheet("用户信息")
                .doWrite(users);
    }
}
